package com.medical.sjq.controller;
import com.github.pagehelper.PageInfo;
import com.medical.service.IllnessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/illness")
public class IllnessController {

    @Autowired
    private IllnessService illnessService;


    @RequestMapping("/getIllnessList")
    public String getAreaList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String areaName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

//        PageInfo<TbIllness> pageInfo = illnessService.getAllIllness(illnessName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", illnessName);

        return "/illness/illness_list";
    }

    @RequestMapping("/openAdd")
    public String openAdd(Model model) {
        return "/illness/illness_add";
    }

    @RequestMapping("/openEdit")
    public String openEidt(Integer id, Model model) {
        TbIllness illness = illnessService.getIllnessById(id);

        model.addAttribute("Illness", illness);
        return "/illness/illness_edit";
    }

    @RequestMapping("/add")
    public String add(TbIllness illness, HttpServletRequest request, Model model) {
        illnessService.add(illness);
        return getIllnessList(1, 10, request, model);
    }

    @RequestMapping("/edit")
    public String edit(TbIllness illness, HttpServletRequest request, Model model) {
        illnessService.edit(illness);
        return getAreaList(1, 10, request, model);
    }

    @RequestMapping("/delete")
    public String delete(Integer id, HttpServletRequest request, Model model) {
        illnessService.delete(id);
        return getIllnessList(1, 10, request, model);
    }

    @ResponseBody
    @RequestMapping("getIllnessTreeList")
    public Object getAreaTreeList() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("illnessList", illnessService.getIllnessTree(""));
        return map;
    }

}
